package com.iplant.aps.controller.aps;

import com.iplant.aps.service.APSService;
import com.iplant.aps.service.po.aps.APSTaskPartPointDay;
import com.iplant.aps.service.po.aps.dayPlan.APSTaskPartPointDayParent;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.basic.controller.BasicController;
import com.iplant.basic.service.po.bms.BMSEmployee;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/APSTaskPartPointDay")
public class APSTaskPartPointDayController extends BasicController<APSTaskPartPointDay> {
    private static final Logger logger = LoggerFactory.getLogger(APSTaskPartPointDayController.class);

    @Autowired
    APSService mAPSService;

    public APSTaskPartPointDayController() {
        super(APSTaskPartPointDay.class);
    }

    /**
     * 查询日计划（未制定）
     */
    @GetMapping("/QueryDayPlan")
    public Object QueryDayPlan(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            Calendar wDate = StringUtils.parseCalendar(request.getParameter("Date"));

            Map<String, Object> wParams = GetParamsMap(request);
            Pagination wPagination = GetPagination(request);

            ServiceResult<List<APSTaskPartPointDay>> wServiceResult = mAPSService.APS_QueryDayPlan(wLoginUser,
                    wParams, wDate, wPagination);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, wPagination.getTotalPage());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    /**
     * 查询日计划（未制定）（计划员）
     */
    @GetMapping("/QueryDayPlanParent")
    public Object QueryDayPlanParent(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            Calendar wDate = StringUtils.parseCalendar(request.getParameter("Date"));

            Map<String, Object> wParams = GetParamsMap(request);
            Pagination wPagination = GetPagination(request);

            ServiceResult<List<APSTaskPartPointDayParent>> wServiceResult =
                    mAPSService.APS_QueryDayPlanParent_V1(wLoginUser, wParams, wDate, wPagination);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, wPagination.getTotalPage());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    /**
     * 查询派工任务（已下达）（班组长）（暂时不用）
     */
    @GetMapping("/QueryDispatchList")
    public Object QueryDispatchList(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            Map<String, Object> wParams = GetParamsMap(request);
            Pagination wPagination = GetPagination(request);

            ServiceResult<List<APSTaskPartPointDayParent>> wServiceResult =
                    mAPSService.APS_QueryDispatchList(wLoginUser, wParams, wPagination);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, wPagination.getTotalPage());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    /**
     * 条件查询记录
     */
    @Override
    @GetMapping("/All")
    public Object All(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            Map<String, Object> wParams = GetParamsMap(request);
            Pagination wPagination = GetPagination(request, mClazz);

            ServiceResult<List<APSTaskPartPointDay>> wServiceResult =
                    mAPSService.APS_QueryTaskPartPointDayAll(wLoginUser, wParams, wPagination);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, wPagination.getTotalPage());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    /**
     * 查询待派工日计划列表
     */
    @GetMapping("/QueryToDispatchList")
    public Object QueryToDispatchList(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            Map<String, Object> wParams = GetParamsMap(request);
            Pagination wPagination = GetPagination(request, mClazz);

            ServiceResult<List<APSTaskPartPointDay>> wServiceResult = mAPSService.APS_QueryToDispatchList(wLoginUser,
                    wParams, wPagination);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    /**
     * 创建日计划
     */
    @PostMapping("/CreateDayPlan")
    public Object CreateDayPlan(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            Calendar wSTime = Calendar.getInstance();
            wSTime.set(wSTime.get(Calendar.YEAR), wSTime.get(Calendar.MONTH), wSTime.get(Calendar.DATE), 0, 0, 0);
            wSTime.add(Calendar.SECOND, -1);

            // 获取参数
            int wTaskPartPointID = StringUtils.parseInt(wParam.get("TaskPartPointID"));
            Calendar wDate = StringUtils.parseCalendar(wParam.get("Date"));
            if (wTaskPartPointID <= 0 || wDate.compareTo(wSTime) < 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<List<APSTaskPartPointDay>> wServiceResult = mAPSService.APS_CreateDayPlan(wLoginUser,
                    wTaskPartPointID, wDate);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    /**
     * 重置首检状态
     */
    @GetMapping("/ResetFirstCheckStatus")
    public Object ResetFirstCheckStatus(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wDayTaskID = StringUtils.parseInt(request.getParameter("DayTaskID"));
            if (wDayTaskID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<Integer> wServiceResult = mAPSService.APS_ResetFirstCheckStatus(wLoginUser, wDayTaskID);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

}
